package com.example.utils;
/*
 * @Author: hx
 * @CreateTime: 2022/3/4 16:55
 * @Description:
 * */

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;

import java.io.*;

public class OSSUtils {

    /**
     *
     */
    private static String ACCESSKEYID = "LTAI5tAtKU321ZgMTyVpXHsJ";
    private static String SECRET = "UDKkczE5wjH4a321XoKMxTCDZHojS2";

    /**
     * 上传文件
     * @param file
     * @param path
     * @param filename
     * @return
     * @throws FileNotFoundException
     */
    public static String upload(File file, String path, String filename) throws FileNotFoundException {
        return upload(new FileInputStream(file),path,filename);
    }

    public static String upload(InputStream inputStream,String path,String filename){
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, ACCESSKEYID, SECRET);
        // 填写Bucket名称，例如examplebucket。
        String bucketName = "yingxue-cloudtest";
        // 填写文件名。文件名包含路径，不包含Bucket名称。例如exampledir/exampleobject.txt。
        String key = path + "/" + filename;
        ossClient.putObject(bucketName, key, inputStream);
        // 关闭OSSClient。
        ossClient.shutdown();
        return "https://" + bucketName + ".oss-cn-hangzhou.aliyuncs.com/" + key;
    }
}
